package com.o2opuzi.pub.controller;

import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.o2opuzi.lib.util.CaptchaUtils;
import com.o2opuzi.lib.util.RandomStringUtils;


/**
 * 验证吗
 * 
 * @author yue han jiang
 * @date 2015年4月13日
 * @file_name CaptchaController.java
 */
@Controller
public class CaptchaController {

	private static Log log = LogFactory.getLog(CaptchaController.class);


	/**
	 * 获取验证吗图片
	 * 
	 * @author yue han jiang
	 * @param key
	 * @param response
	 */
	@RequestMapping(value = { "/captcha" }, method = RequestMethod.GET)
	@ResponseBody
	public void captcha(HttpServletResponse response,HttpSession session) {
		OutputStream out = null;
		try {
			out = response.getOutputStream();
			response.setHeader("Cache-Control", "no-store");
			response.setHeader("Pragma", "no-cache");
			response.setDateHeader("Expires", 0);
			response.setContentType("image/jpeg");
			String code = RandomStringUtils.randomNum(4);
			session.setAttribute("captcha", code);
			CaptchaUtils.outputImage(100, 44, new Color(0, 168, 255, 255), out,code);
		} catch (Exception e) {
			log.error(e);
		} finally {
			try {
				if (out != null)
					out.flush();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (out != null)
						out.close();
				} catch (IOException e) {
					log.error(e);
				}
			}
		}
	}
}
